<template>
  <div class="dashboard-container">
    <div class="app-container">
      <el-card>
        <el-tabs v-model="activeName" @tab-click="loadDetail">
          <el-tab-pane label="登录账户设置" name="account">
            <el-row type="flex" justify="center" style="margin-right:200px">
              <el-form label-width="100px">
                <el-form-item label="姓名">
                  <el-input v-model="formData.username" />
                </el-form-item>
                <el-form-item label="密码">
                  <el-input v-model="formData.newPassword" type="password" />
                </el-form-item>
                <el-form-item>
                  <el-button type="primary" @click="updateAccount">更新</el-button>
                  <el-button @click="$router.back()">返回</el-button>
                </el-form-item>
              </el-form>
            </el-row>
          </el-tab-pane>
          <el-tab-pane label="个人详情" name="detail">
            <component :is="userInfoName" ref="userDetail" />
          </el-tab-pane>
          <el-tab-pane label="岗位信息" name="job">
            <component :is="jobInfoName" ref="job" />
          </el-tab-pane>
        </el-tabs>
      </el-card>
    </div>
  </div>
</template>

<script>
import { getUserDetailById } from '@/api/user'
import { saveEmployeeDetailById } from '@/api/employees'
import JobInfo from '../components/job-info'
import UserInfo from '../components/user-info'
export default {
  components: {
    JobInfo,
    UserInfo
  },
  data() {
    return {
      // 动态组件
      userInfoName: 'UserInfo',
      jobInfoName: 'JobInfo',
      activeName: 'account',
      formData: {
        username: '',
        newPassword: ''
      },
      userId: this.$route.params.id
    }
  },
  created() {
    this.getUserDetailById()
  },
  methods: {
    async getUserDetailById() {
      this.formData = await getUserDetailById(this.userId)
      // console.log(this.formData)
    },
    async updateAccount() {
      await saveEmployeeDetailById({ ...this.formData, password: this.formData.newPassword })
      this.$message.success('修改成功')
      this.formData.newPassword = ''
    },
    async loadDetail(obj) {
      console.log(obj.name, '我被点击了')
      // const data = obj.getPersonalDetail()
      if (obj.name === 'detail') {
        // console.log(this.$refs.userDetail)
        await this.$refs.userDetail.getPersonalDetail()
        await this.$refs.userDetail.getUserDetailById()
      } else if (obj.name === 'job ') {
        await this.$refs.job.getJobDetail()
      }
    }
  }
}
</script>

<style>

</style>
